package com.office.sysManage.deptManage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.sysManage.deptManage.dao.DeptMapper;
import com.office.sysManage.deptManage.pojo.Dept;
import com.office.sysManage.deptManage.service.DeptService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/27
 * 星期四 14:35
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    public DeptMapper getDeptMapper() {
        return deptMapper;
    }
    public void setDeptMapper(DeptMapper deptMapper) {
        this.deptMapper = deptMapper;
    }


    /**
     * 查询部门信息
     * @return 部门列表
     */
    @Override
    public PageModel<Dept> queryDept(Dept dept, PageModel<Dept> pageModel){
        if (dept != null){
            if ("%".equals(dept.getDeptName()) || "_".equals(dept.getDeptName())){
                dept.setDeptName(null);
            }
            pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
            pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
            PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

            List<Dept> deptList = deptMapper.queryDept(dept);

            PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);

            pageModel.setData(new ArrayList<Dept>(deptList));
            pageModel.setTotalPage(pageInfo.getPages());
            pageModel.setTotalRows(pageInfo.getTotal());
            pageModel.setNavigatepageNums(pageInfo.getNavigatepageNums());
            pageModel.setFirstPage(pageInfo.isIsFirstPage());
            pageModel.setLastPage(pageInfo.isIsLastPage());

            return pageModel;
        }
        return null;
    }

    /**
     * 查询部门信息
     * @return 部门列表
     */
    @Override
    public List<Dept> queryDeptListForUpdate(Dept dept) {
        // 判断当前部门是否为一级部门
        int deptLevel = deptMapper.queryDeptInfo(dept).getDeptLevel();
        if (deptLevel == 1){
            ArrayList<Dept> depts = new ArrayList<>();
            depts.add(dept);
            return depts;
        }
        // 不是一级部门,获取当前部门所有上一级别的部门
        return queryParentDeptByDeptName(dept);
    }

    @Override
    public List<Dept> queryDeptListForAdd(Dept dept) {
        return deptMapper.queryDept(dept);
    }

    /**
     * 获取当前部门的所有上一等级的部门
     * @param dept 部门对象
     * @return 部门集合
     */
    private List<Dept> queryParentDeptByDeptName(Dept dept){
        // 获取当前部门等级
        Dept dept1 = deptMapper.queryDeptInfo(dept);
        Dept dept2 = new Dept();
        dept2.setDeptLevel(dept1.getDeptLevel());
        return deptMapper.queryDept(dept2);
    }

    /**
     * 添加部门
     * @param dept 部门对象
     * @return 是否添加成功
     */
    @Override
    public boolean addDept(Dept dept) {
        if (dept != null){
            // 设置父级部门ID
            Dept dept1 = new Dept();
            dept1.setDeptName(dept.getDeptFName());
            int fId = deptMapper.queryDeptIdByName(dept1);
            dept.setDeptFid(fId);
            // 设置当前部门级别
            Dept dept2 = deptMapper.queryDeptInfo(dept1);
            int deptLevel = dept2.getDeptLevel();
            dept.setDeptLevel(deptLevel+1);
            return deptMapper.addDept(dept) >= 1;
        }
        return false;
    }

    /**
     * 修改部门名称
     * @param dept 部门对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateDept(Dept dept) {
        if (dept != null){
            Dept dept1 = deptMapper.queryDeptInfo(dept);
            dept.setId(dept1.getId()); // 设置部门ID
            Dept dept2 = new Dept();
            dept2.setDeptName(dept.getDeptFName());
            Dept dept3 = deptMapper.queryDeptInfo(dept2);
            dept.setDeptFid(dept3.getId());
            return deptMapper.updateDept(dept) >= 1;
        }
        return false;
    }

    /**
     * 逻辑删除部门
     * @param dept 部门对象
     * @return 是否删除成功
     */
    @Override
    public boolean deleteDept(Dept dept) {
        if (dept != null){
            return deptMapper.deleteDept(dept) >= 1;
        }
        return false;
    }

    /**
     * 根据父级部门名称查询部门ID
     * @param dept 部门对象
     * @return 父级部门ID
     */
    @Override
    public int queryDeptIdByName(Dept dept) {
        return deptMapper.queryDeptIdByName(dept);
    }

    /**
     * 根据部门名称查询部门信息
     * @param dept 部门对象
     * @return 部门对象
     */
    @Override
    public Dept queryDeptInfo(Dept dept) {
        if (dept != null){
            Dept dept1 = deptMapper.queryDeptInfo(dept); // 查询出fid
            if (dept1.getDeptFid() != 0){
                Dept dept2 = new Dept();
                dept2.setId(dept1.getDeptFid());
                if (deptMapper.queryDept(dept2).size() > 0) {
                    Dept dept3 = deptMapper.queryDept(dept2).get(0); // 查询出fid对应的信息
                    dept1.setDeptFName(dept3.getDeptName()); // 拿到父级部门名称
                }
            }else {
                dept1.setDeptFName("没有上级部门"); // 设置父级部门名称
            }
            return dept1;
        }
        return null;
    }

    /**
     * 查询所有区域
     * @return 部门对象
     */
    @Override
    public List<Dept> queryArea() {
        return deptMapper.queryArea();
    }

}
